package com.squirrel.michale;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/2/22 7:16 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2007 {

    public int[] findOriginalArray(int[] changed) {

        if (changed.length % 2 != 0) {
            return new int[0];
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });

        for (int i = 0; i < changed.length; i++) {
            heap.add(changed[i]);
        }

        int[] originalArray = new int[changed.length / 2];
        for (int i = 0; i < originalArray.length; i++) {
            Integer max = heap.poll();
            if (max % 2 != 0) {
                return new int[0];
            }
            int original = max / 2;
            if (!heap.contains(original)){
                return new int[0];
            }
            heap.remove(original);
            originalArray[i] = original;
        }
        return originalArray;

    }
}
